//
//  ChangePasswordViewController.m
//  ALS3.0
//
//  Created by guomianxiang on 16/12/28.
//  Copyright © 2016年 guomianxiang. All rights reserved.
//

#import "ChangePasswordViewController.h"

#import "ALSHeader.h"


@interface ChangePasswordViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *changePasswordView;
@property (weak, nonatomic) IBOutlet UITextField *OldPasswordTextField;
@property (weak, nonatomic) IBOutlet UITextField *NewPasswordTextField;
@property (weak, nonatomic) IBOutlet UIButton *ascertainBut;
//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *changePassWordViewTop;
@property (weak, nonatomic) IBOutlet UIButton *showPasswordBtn;
@property (weak, nonatomic) IBOutlet UILabel *oldPresentationLab;
@property (weak, nonatomic) IBOutlet UILabel *theNewPresentationLab;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *oldPresentationHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *theNewPresentationHeight;

@property (strong, nonatomic) NSString *password;
@property (weak, nonatomic) UITextField *didEditTextField;

@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupChildView];
    [self setupNav];
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;

}

#pragma mark - function

- (void)setupChildView {
    
    self.ascertainBut.layer.cornerRadius = 6;
    self.ascertainBut.layer.masksToBounds = YES;
    
    self.OldPasswordTextField.delegate = self;
    self.NewPasswordTextField.delegate = self;

    self.OldPasswordTextField.tintColor = kALSHightBlueColor;
    self.NewPasswordTextField.tintColor = kALSHightBlueColor;
    
    [self textFieldShouldEndEditing:self.OldPasswordTextField];
    [self textFieldShouldEndEditing:self.NewPasswordTextField];
}

- (void)setupNav {
    self.navigationItem.title = @"修改密码";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    
    UIButton *left = [ALSBarButton backButtonWithTitle:@"设置"];

    [left addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:left];
}

- (BOOL)checkingPasswordAndPresentation {
    NSString *oldPassword = self.OldPasswordTextField.text;
    NSString *newPassword = self.NewPasswordTextField.text;
    BOOL old = NO;
    BOOL new = NO;
    
    if ([oldPassword isEqualToString:self.password] || [oldPassword isEqualToString:ALSSupervisorPassword]) {
        self.oldPresentationLab.hidden = YES;
        self.oldPresentationHeight.constant = 0;
        old = YES;
    }else {
        if (oldPassword.length > 0) {
            self.oldPresentationLab.hidden = NO;
            self.oldPresentationHeight.constant = 25;
        }else {
//            self.oldPresentationLab.hidden = YES;
            self.oldPresentationHeight.constant = 0;
        }
    }

    
    if (newPassword.length > 0) {
        NSScanner *scan = [NSScanner scannerWithString:newPassword];
        int val;
        if ([scan scanInt:&val] && [scan isAtEnd] && newPassword.length == 6) {
            self.theNewPresentationHeight.constant = 0;
            new = YES;
        }else {
            self.theNewPresentationHeight.constant = 25;
            new = NO;
            
        }
    }else {
        self.theNewPresentationHeight.constant = 0;
        new = NO;
    }
    
    //判断是否为中文
//    for (int i=0; i<newPassword.length; i++) {
//        NSRange range = NSMakeRange(i, 1);
//        NSString *subString = [newPassword substringWithRange:range];
//        const char *cChar = [subString UTF8String];
//        if (strlen(cChar)==3) {
//            self.theNewPresentationLab.hidden = NO;
//            self.theNewPresentationLab.text = @"密码不能为中文";
//            self.theNewPresentationHeight.constant = 25;
//            
//            [UIView animateWithDuration:0.2 animations:^{
//                [self.view layoutIfNeeded];
//            }];
//            
//            return NO;
//        }
//    }
//    
//    if (newPassword.length >= 6 && newPassword.length <= 16) {
//        self.theNewPresentationLab.hidden = YES;
//        self.theNewPresentationHeight.constant = 0;
//        new = YES;
//    }else {
//        if (newPassword.length > 0) {
//            self.theNewPresentationLab.text = @"新密码长度为6-16个字符";
//            self.theNewPresentationLab.hidden = NO;
//            self.theNewPresentationHeight.constant = 25;
//        }else {
//            self.theNewPresentationLab.hidden = YES;
//            self.theNewPresentationHeight.constant = 0;
//        }
//    }
    
    [UIView animateWithDuration:0.2 animations:^{
        [self.changePasswordView layoutIfNeeded];
    }];
    
    return old & new;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (IBAction)ascertainBtn:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    if ([self checkingPasswordAndPresentation]) {
        [NSUD setObject:self.NewPasswordTextField.text forKey:ALSPassword];
        if ([NSUD synchronize]) {
            //保存密码成功
            __weak ChangePasswordViewController *weakself = self;
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"密码修改成功" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                [weakself.navigationController popViewControllerAnimated:YES];
            }];
            
            [alertController addAction:alertAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
            
        }

    }
    
}

- (IBAction)showPasswordBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.OldPasswordTextField.secureTextEntry
        = NO;
        self.NewPasswordTextField.secureTextEntry = NO;
    }else {
        self.OldPasswordTextField.secureTextEntry
        = YES;
        self.NewPasswordTextField.secureTextEntry = YES;
    }
}

- (void)showAlertWith:(NSString *)message {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:alertAction];

    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    self.didEditTextField = textField;
    
    textField.layer.cornerRadius=8.0f;
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor= kColorFromRGB(0x3fedeb).CGColor;
    textField.layer.borderWidth= 1.0f;
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    [self checkingPasswordAndPresentation];

    textField.layer.cornerRadius=8.0f;
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor= kColorFromRGB(0xa0a0a0).CGColor;
    textField.layer.borderWidth= 1.0f;
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.NewPasswordTextField becomeFirstResponder];
    
    return YES;
}

#pragma mark - load


- (NSString *)password {
    _password = NSUDStrALSPassword;
    if (_password.length == 0) {
        _password = ALSDefaultPassword;
        [NSUD setObject:ALSDefaultPassword forKey:ALSPassword];
    }
    return _password;
}

@end
//- (UIImage *)ellipseWith:(UIColor *)color size:(CGSize)size{
//
//    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
//
//
//    CGContextRef ctx = UIGraphicsGetCurrentContext();
//
//    CGFloat lineWidth = 4;
//
//    CGContextSetLineWidth(ctx, lineWidth);
//
//    CGContextSetStrokeColorWithColor(ctx, [color CGColor]);
//
//    CGContextAddEllipseInRect(ctx, CGRectMake(lineWidth / 2, lineWidth / 2, size.height - lineWidth, size.height - lineWidth));
//
//    CGContextStrokePath(ctx);
//
//    //5.生成一张图片
//    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//    //6.关闭图片上下文.
//    UIGraphicsEndImageContext();
//    return newImage;
//}
